import Vue from "vue";
import VueRouter from "vue-router";
import axios from "axios";
import qs from "qs";
import ElementUI from "element-ui";
import VueAxios from "vue-axios";
import 'element-ui/lib/theme-chalk/index.css';
import Login from "./components/Login";
import ResourcesSave from "./components/ResourcesSave";

Vue.use(VueRouter);
Vue.prototype.$axios = axios
Vue.prototype.qs = qs
Vue.use(ElementUI,VueAxios,axios);

const router = new VueRouter({
    routes: [
        {
            path: '/',
            name: 'login',
            alias: '/login',
            component: Login
        },
        {
            path: '/resources-save',
            name: 'resourcesSave',
            meta: {
                requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
            component: ResourcesSave
        },
    ]
});

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (window.localStorage.getItem('token')) {  //获取当前的token是否存在
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }
    else {
        next();
    }
});

//配置导出路由
export default router;